package com.teddy.view.tree;

import java.io.IOException;

import org.eclipse.swt.SWT;
import org.eclipse.swt.events.MouseAdapter;
import org.eclipse.swt.events.MouseEvent;
import org.eclipse.swt.graphics.Point;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Tree;
import org.eclipse.swt.widgets.TreeItem;

import com.teddy.model.abstracts.SemanticObject;

public class SemanticTree extends Tree{
	
	private SemanticTreeItem selectedItem;
	
	public SemanticTree(Composite parent, int style) {
		super(parent, style);
		addExpandListener();
		addMouseClickListener();
		//selectedItem = new SemanticT
	}
	
	public void addMouseClickListener(){
		addMouseListener(new MouseAdapter() {
			public void mouseDown(MouseEvent event) {
				if (event.button == 1){
					Point point = new Point (event.x, event.y);
					SemanticTreeItem si = (SemanticTreeItem)getItem (point);
					selectedItem = si;
				}
			}
		});
	}
	public void addExpandListener(){
	}
	
	public void addRightClickListener(){
		
	}
	/*
	 treeHierarchy.addListener( SWT.Expand, new Listener() {
			
			@Override
			public void handleEvent(Event e) {
				SemanticTreeItem selectedItem = (SemanticTreeItem) e.item;
				try {
					selectedItem.expand();
				} catch (Exception e1) {
					// TODO Auto-generated catch block
					e1.printStackTrace();
				}
			}
		});
		
		treeHierarchy.addMouseListener(new MouseAdapter() {
			public void mouseDown(MouseEvent event) {
			if (event.button == 3){
				Point point = new Point (event.x, event.y);
				final SemanticTreeItem item = (SemanticTreeItem) treeHierarchy.getItem (point);
				if (item != null) {
					System.out.println ("Mouse down: " + item);
					
					Menu menu = new Menu(treeHierarchy.getShell(), SWT.POP_UP);
					     MenuItem removeItem = new MenuItem (menu, SWT.PUSH);
					      removeItem.setText ("remove");
					      MenuItem refreshItem = new MenuItem (menu, SWT.PUSH);
					      refreshItem.setText ("refresh");
					      
		
						   /* MenuItem addItem = new MenuItem (menu, SWT.PUSH);
						    removeItem.setText ("add");
						    MenuItem rename = new MenuItem (menu, SWT.PUSH);
						    refreshItem.setText ("rename");*/
					
					      //draws pop up menu:
					     //pt = table.toDisplay (pt);
					   /*  menu.setLocation (point.x, point.y);
					     menu.setVisible (true);
					     removeItem.addListener(SWT.MouseUp, new Listener(){

							@Override
							public void handleEvent(Event event) {
								try {
									item.remove();
								} catch (Exception e) {
									e.printStackTrace();
								}
							}
					    	 
					     });
					     
					     refreshItem.addListener(SWT.MouseDown, new Listener(){

								@Override
								public void handleEvent(Event event) {
									try {
										item.refresh();
									} catch (Exception e) {
										// TODO Auto-generated catch block
										e.printStackTrace();
									}
								}
						    	 
						     });
				}
			}
			}
		});
	 */
	public void importModel(SemanticObject so) throws IOException{
		//SemanticObject so = manager.importThesaurus(uri);
		ModelTreeItem item = new ModelTreeItem(this, SWT.NONE, so);
		item.expand();
		redraw();
	}
	
	public SemanticTreeItem getSelectedItem(){
		return selectedItem;
	}
	
	public void exportModel(){	
	}
	
	public void renameModel(){
		
	}
	
	
	protected void checkSubclass() {
		// Disable the check that prevents subclassing of SWT components
	} 
	
	public TreeItem getItem(int index) {
		return (TreeItem)super.getItem(index);
	}

	public void setSelectedItem(ModelTreeItem selectedItem) {
		this.selectedItem = selectedItem;
	}

}
